package Test01;

import cn.itcast.xml.XmlUtils;

import java.util.*;

/**
 * Created by 14229 on 2020/2/25.
 */
public class Test {
    public static void main(String[] args) {

        List<Map<String, String>> zhangsan = getStuScore("liSi", "");
        for (Map<String, String> stringStringMap : zhangsan) {
            System.out.println(stringStringMap);
        }
    }
    public static List<Map<String,String>> getStuScore(String sname,String cname){
        List<Map<String,String>> list = new ArrayList<>();
        //代码填充
        List<Student> student = getStudent();
        List<Coures> course = getCourse();
        List<Score> score = getScore();
        for (Student stu : student) {
            for (Coures cou : course) {
                for (Score sco : score) {
                    if (stu.getSname().equals(sname)){
                        if (cou.getCname().equals(cname) && cou.getCid().equals(sco.getCid()) && stu.getSid().equals(sco.getSid())){
                            Map<String,String> map = new HashMap<>();
                            map.put("sname",stu.getSname());
                            map.put("cname",cou.getCname());
                            map.put("score",sco.getScore()+"");
                            list.add(map);
                            break;
                        }else if (cname==null || cname.equals("")){
                            if (stu.getSid().equals(sco.getSid()) && sco.getCid().equals(cou.getCid())){
                                Map<String,String> map = new HashMap<>();
                                map.put("sname",stu.getSname());
                                map.put("cname",cou.getCname());
                                map.put("score",sco.getScore()+"");
                                list.add(map);
                            }

                        }
                    }
                }
            }
        }
        Collections.sort(list, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                int s1 = Integer.parseInt( o1.get("score"));
                int s2 = Integer.parseInt( o2.get("score"));
                return s1-s2;
            }
        });
        return list;
    }
    public static List<Student> getStudent(){
        //代码填充
      return XmlUtils.readAll("student.xml",Student.class);
    }
    public static List<Coures> getCourse(){
        //代码填充
        List<Coures> coures = XmlUtils.readAll("course.xml", Coures.class);
        Set<Coures> set = new HashSet<>(coures);
        coures.clear();
        coures.addAll(set);
        return coures;
    }
    public static List<Score> getScore(){
        //代码填充
       return XmlUtils.readAll("score.xml",Score.class);
    }
}
